package io.renren.modules.pinkao.controller;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.pinkao.entity.PinKaoTeamAndParent;
import io.renren.modules.sys.entity.SysMenuEntity;
import org.apache.commons.codec.binary.Base64;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.pinkao.entity.PinKaoCourseEntity;
import io.renren.modules.pinkao.service.PinKaoCourseService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author xuebing
 * @email xuebinggod@163.com
 * @date 2018-12-14 14:41:51
 */
@RestController
@RequestMapping("pinkao/pinkaocourse")
public class PinKaoCourseController {
    @Autowired
    private PinKaoCourseService pinKaoCourseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("pinkao:pinkaocourse:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = pinKaoCourseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{pinKaoCourseId}")
  //  @RequiresPermissions("pinkao:pinkaocourse:info")
    public R info(@PathVariable("pinKaoCourseId") Integer pinKaoCourseId){
        PinKaoCourseEntity pinKaoCourse = pinKaoCourseService.selectById(pinKaoCourseId);

        return R.ok().put("pinKaoCourse", pinKaoCourse);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("pinkao:pinkaocourse:save")
    public R save(@RequestBody PinKaoCourseEntity pinKaoCourse) throws UnsupportedEncodingException {
      //  PinKaoCourseEntity pinKaoCourse=new PinKaoCourseEntity();
        //pinKaoCourse.set
        Base64 base64 = new Base64();

        pinKaoCourse.setCourseImage(new String(base64.decode(pinKaoCourse.getCourseImage()), "UTF-8"));

        pinKaoCourseService.insert(pinKaoCourse);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("pinkao:pinkaocourse:update")
    public R update(@RequestBody PinKaoCourseEntity pinKaoCourse){
        ValidatorUtils.validateEntity(pinKaoCourse);
        pinKaoCourseService.updateAllColumnById(pinKaoCourse);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("pinkao:pinkaocourse:delete")
    public R delete(@RequestBody Integer[] pinKaoCourseIds){
        pinKaoCourseService.deleteBatchIds(Arrays.asList(pinKaoCourseIds));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/online")
    @RequiresPermissions("course:course:delete")
    public R online(@RequestBody PinKaoCourseEntity pinKaoCourseEntity){
        //  courseService.deleteBatchIds(Arrays.asList(ids));
        //PinKaoCourseEntity pinKaoCourseEntity=new PinKaoCourseEntity();

        pinKaoCourseService.updateById(pinKaoCourseEntity);
        return R.ok();
    }

    /**
     * 所有菜单列表
     */
    @RequestMapping("/getPinkaoTeamList")
    public List<PinKaoTeamAndParent> list(Integer pinKaoId) throws UnsupportedEncodingException {

        return pinKaoCourseService.getPinKaoTeamAndParentList(pinKaoId);
    }


}
